<?php

/* vim: set expandtab tabstop=4 shiftwidth=4: */

// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

Class MemberAction extends backendAction {

    static $Mtime;
    var $filename162;
    var $filename48;
    var $filename20;
    var $path;
    var $DB_PREFIX;
    var $Api;
    var $json;

    public function _initialize() {

        $this->Api = A("Api");

        $Mtime = $this->getMillisecond();

        $this->filename162 = $Mtime . "_big.png";

        $this->filename48 = $Mtime . "_normal.png";

        $this->filename20 = $Mtime . "_small.png";

        $this->path = "upload/Portrait/";

        $this->DB_PREFIX = C('DB_PREFIX');
        
        $this->json = (isset($_REQUEST['json'])) ? $_REQUEST['json'] :0 ;
         
        if (!$_SESSION['uid']) {
            if($this->json!=0){
                 $this->Api->faileres("操作失败!");
            }else{
                 $this->redirect_ext();
            }
           
        }
        else{
             $tempArr = $this->Api->get_members($_SESSION['uid']);
            $review = $tempArr[review];
             $this->assign('review', $review);
        }
    }

    public function index() {
        $this->navigation('我的资料');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_email', $membersInfo[email]);
            $this->assign('Example_regdate', $membersInfo[regdate]);
            $this->assign('Example_nicheng', $membersInfo[nicheng]);
            $this->assign('Example_lastlogintime', $membersInfo[lastlogintime]);
            $this->assign('Example_balance', $membersInfo[balance]);

            $portrait = unserialize($membersInfo[portrait]);


            $this->assign('Example_portrait', $portrait[0]);
            // print_r($membersInfo);exit();
            // 
            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function follow() {
       
        $this->navigation('我的关注');
        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_email', $membersInfo[email]);
            $this->assign('Example_regdate', $membersInfo[regdate]);
            $this->assign('Example_nicheng', $membersInfo[nicheng]);
            $this->assign('Example_lastlogintime', $membersInfo[lastlogintime]);
            // print_r($membersInfo);exit();
            // 
            // 
            $map[$this->DB_PREFIX . 'collect.uid'] = $members[uid];


            $list = M("members")->join($this->DB_PREFIX . 'collect ON ' . $this->DB_PREFIX . 'members.uid =' . $this->DB_PREFIX . 'collect.item_id')->where($map)->findAll();

            foreach ($list as $key => $value) {
                $list[$key][portrait] = unserialize($value[portrait]);
            }
            if($this->json!=0){
                $list ? $this->Api->successres("操作成功",$list ) : $this->Api->faileres("操作失败!");
            }else{
            $this->assign('list', $list);
            $this->display(); }
        } else {
                if($this->json!=0){
                 $this->Api->faileres("操作失败!");
            }else{
                 $this->redirect_ext();
            }
           
        }
    }

    //删除关注
    public function collect_delete() {

        $members[uid] = $_SESSION['uid'];
        $item_id = $_REQUEST['item_id'];
        $map['item_id'] = $item_id;
        $map["uid"] = $members[uid];

        $map['_logic'] = 'and';



        M("collect")->where($map)->delete();

        $this->redirect(U('Member/follow'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
    }

    public function be_follow() {
        $this->navigation('关注我的');
        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_email', $membersInfo[email]);
            $this->assign('Example_regdate', $membersInfo[regdate]);
            $this->assign('Example_nicheng', $membersInfo[nicheng]);
            $this->assign('Example_lastlogintime', $membersInfo[lastlogintime]);
            // print_r($membersInfo);exit();
            // 
            // 
            $map[$this->DB_PREFIX . 'collect.item_id'] = $members[uid];


            $list = M("members")->join($this->DB_PREFIX . 'collect ON ' . $this->DB_PREFIX . 'members.uid =' . $this->DB_PREFIX . 'collect.uid')->where($map)->findAll();

            foreach ($list as $key => $value) {
                $list[$key][portrait] = unserialize($value[portrait]);
            }
            $this->assign('list', $list);
            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function collect_edit_act() {


        $is_manager = $_REQUEST[v];

        $data = array(
            'is_manager' => $is_manager,
        );


        $map['uid'] = $_REQUEST[uid];

        M("collect")->where($map)->save($data);




        $this->redirect(U('/Member/be_follow'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
    }

    public function userimg() {
        $this->navigation('头像修改');
        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_email', $membersInfo[email]);
            $this->assign('Example_regdate', $membersInfo[regdate]);
            $this->assign('Example_nicheng', $membersInfo[nicheng]);
            $this->assign('Example_lastlogintime', $membersInfo[lastlogintime]);
            $this->assign('bigPortrait', $this->path . $this->filename162);
            $this->assign('normalPortrait', $this->path . $this->filename48);
            $this->assign('smallPortrait', $this->path . $this->filename20);

            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function redirect_ext() {

        $this->redirect(U('/index/'), array(), 2, ' 您尚未登录!,2秒钟后页面跳转中 ~');
    }

    // 处理表单数据
    public function upfile() {

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $file_src = "src.png";


            $src = base64_decode($_POST['pic']);
            $pic1 = base64_decode($_POST['pic1']);
            $pic2 = base64_decode($_POST['pic2']);
            $pic3 = base64_decode($_POST['pic3']);

            if ($src) {
                file_put_contents($file_src, $src);
            }

            file_put_contents($this->path . $this->filename162, $pic1);
            file_put_contents($this->path . $this->filename48, $pic2);
            file_put_contents($this->path . $this->filename20, $pic3);

            $rs['status'] = 1;


            $portrait = array(0 => $this->filename162,
                1 => $this->filename48,
                2 => $this->filename20,
            );

            $portrait = serialize($portrait);



            $kros = M("members");

            $map['uid'] = $members[uid];

            $data['portrait'] = $portrait;

            $kros->where($map)->save($data);






            echo json_encode($rs);
        }
    }

    // 处理表单数据
    public function krosPath() {
        $path = APP_PATH . "Uploads/";

        print_r($path);

        echo $this->getMillisecond();
    }

    function getMillisecond() {
        list($t1, $t2) = explode(' ', microtime());
        return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
    }

    function navigation($navigation = "我的资料") {

        $this->assign('Example_navigation', $navigation);
    }

    // 直播设置
    public function set() {

        $this->navigation('直播设置');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_title', $membersInfo[title]);
            $this->assign('Example_notice', $membersInfo[notice]);
            $this->assign('Example_domainName', $membersInfo[domainName]);
            $this->assign('Example_category_id', $membersInfo[category_id]);
            $this->assign('Example_score', $membersInfo[score]);


            // print_r($membersInfo);exit();
            // 
            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function set_edit() {

        $this->navigation('直播设置修改');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_title', $membersInfo[title]);
            $this->assign('Example_notice', $membersInfo[notice]);
            $this->assign('Example_domainName', $membersInfo[domainName]);
            $this->assign('Example_category_id', $membersInfo[category_id]);
            $this->assign('Example_score', $membersInfo[score]);


            // print_r($membersInfo);exit();
            // 
            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function set_edit_act() {

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        $data = array(
            'title' => $_POST[title],
            'notice' => $_POST[notice],
            'domainName' => $_POST[domainName],
            'category_id' => $_POST[category_id],
        );


        $map['uid'] = $members[uid];

        if (!empty($members[uid])) {


            M("members")->where($map)->save($data);

            $this->redirect(U('/Member/set'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
        } else {
            $this->redirect_ext();
        }
    }

    public function user_edit() {

        $this->navigation('我的资料修改');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_nicheng', $membersInfo[nicheng]);
            $this->assign('Example_email', $membersInfo[email]);



            // print_r($membersInfo);exit();
            // 
            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function user_edit_act() {

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        $data = array(
            'nicheng' => $_POST[nicheng],
            'email' => $_POST[email],
        );


        $map['uid'] = $members[uid];

        if (!empty($members[uid])) {


            M("members")->where($map)->save($data);

            $this->redirect(U('/Member/index'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
        } else {
            $this->redirect_ext();
        }
    }

    // 直播设置
    public function gift() {

        $this->navigation('我的虾球');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            
            $map[uid]= $_SESSION['uid'];
            $memberArr=D('Members')->where($map)->find();
            
            $balance=$memberArr[balance];
            
            $this->assign('balance', $balance);
                  if($this->json!=0){
                 $balance? $this->Api->successres("操作成功",$balance ) : $this->Api->faileres("操作失败!");
            }else{
                $this->display();
            }

            
        } else {
            if($this->json!=0){
             $this->Api->faileres("操作失败!");
            }else{
                  $this->redirect_ext();
            }
          
        }
    }

    public function gift_store() {

        $this->navigation('礼物商城');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $membersInfo = M(members)->where(array(
                        'uid' => $members[uid]
                    ))->find();
            $this->assign('Example_title', $membersInfo[title]);
            $this->assign('Example_notice', $membersInfo[notice]);
            $this->assign('Example_domainName', $membersInfo[domainName]);
            $this->assign('Example_category_id', $membersInfo[category_id]);
            $this->assign('Example_score', $membersInfo[score]);


            // print_r($membersInfo);exit();
            // 
            $this->display();
        } else {
            $this->redirect_ext();
        }
    }

    public function gains() {

        $this->navigation('主播收益明细表');
        if($this->json!=0){       
       $condition['to_id'] = $_SESSION['uid'];
        $arr = D('gift_log')->where($condition)->order('id desc')->findAll();
      foreach ($arr as $key => $val) {

                $gift = $this->Api->get_gift($val[gift_id]);
                $gift_name = $gift['gift_name'];
                $arr[$key]['gift_name'] = $gift_name;
                $arr[$key]['uid'] = get_username($arr[$key]['uid']);
                $arr[$key]['type'] = get_charge_status($arr[$key]['type']);
                 $arr[$key]['give_time'] = date("Y-m-d H:i:s",$arr[$key]['give_time']);

                $sum += $val[worth];
            }
            
               $arr ? $this->Api->successres("操作成功",$arr ) : $this->Api->faileres("操作失败!");
            }else{
                
           
        if ($_REQUEST['time_start']) {
            import("@.ORG.Page"); //导入分页类
            $time_start = strtotime($_REQUEST['time_start']);

            $time_end = strtotime($_REQUEST['time_end']);
            $condition['give_time'] = array(array('egt', $time_start), array('elt', $time_end));
            $condition['to_id'] = $_SESSION['uid'];

            $count = D('gift_log')->where($condition)->count(); //计算总数
            $p = new Page($count, 15);
            $arr = D('gift_log')->where($condition)->limit($p->firstRow . ',' . $p->listRows)->order('id desc')->findAll();

            foreach ($arr as $key => $val) {

                $gift = $this->Api->get_gift($val[gift_id]);
                $gift_name = $gift['gift_name'];
                $arr[$key]['gift_name'] = $gift_name;

                $sum += $val[worth];
            }
            $p->setConfig('header', '篇记录');
            $p->setConfig('prev', "上一页");
            $p->setConfig('next', '下一页');
            $p->setConfig('first', '<<');
            $p->setConfig('last', '>>');
            $page = $p->show();
            $this->assign("page", $page);

            $this->assign('list', $arr);
            $this->assign('sum', $sum);
            return $this->display('gains_list');
        }
        $kros = M("gift_log"); // 实例化模型类
        $map['to_id'] = $_SESSION['uid'];
        import("@.ORG.Page"); //导入分页类
        $count = $kros->where($map)->count(); //计算总数
        $p = new Page($count, 15);

        $list = $kros->where($map)->limit($p->firstRow . ',' . $p->listRows)->order('id desc')->findAll();
        $p->setConfig('header', '篇记录');
        $p->setConfig('prev', "上一页");
        $p->setConfig('next', '下一页');
        $p->setConfig('first', '<<');
        $p->setConfig('last', '>>');
        $page = $p->show();
        $this->assign("page", $page);



        $this->assign('list', $list);
        $this->display('gains_list');
    }
 }
    public function grade() {
  $this->navigation('我的等级');
        $tempArr = $this->Api->get_members($_SESSION['uid']);
        $level = $tempArr[grade_level];

        $grade_score = $tempArr[grade_score];

        $need_score = D('grade')->get_need_score($level);

        $next_need_score = D('grade')->get_need_score($level + 1);

        $this->assign('level', $level);
        $percentage = round(100 * ($grade_score - $need_score) / ($next_need_score - $need_score));

        $this->assign('percentage', $percentage);
        $this->assign('level', $level);
         if($this->json!=0){
                $level ? $this->Api->successres("操作成功",$level ) : $this->Api->faileres("操作失败!");
            }else{
                 $this->display();
            }
       
    }

      public function username() {
        $tempArr = $this->Api->get_members($_SESSION['uid']);
        $level = $tempArr[grade_level];

        $username = $tempArr[username];

        $grade_score = $tempArr[grade_score];

        $need_score = D('grade')->get_need_score($level);

        $next_need_score = D('grade')->get_need_score($level + 1);

        $this->assign('level', $level);
        $percentage = round(100 * ($grade_score - $need_score) / ($next_need_score - $need_score));

        $this->assign('percentage', $percentage);
        $this->assign('level', $level);
         if($this->json!=0){
                $level ? $this->Api->successres("操作成功",$username ) : $this->Api->faileres("操作失败!");
            }else{
                 $this->display();
            }
       
    }

    public function gains_charge() {

        if ($_REQUEST['time_start']) {

            $time_start = strtotime($_REQUEST['time_start']);

            $time_end = strtotime($_REQUEST['time_end']);
            $condition['give_time'] = array(array('egt', $time_start), array('elt', $time_end));

            $condition[to_id] = $_SESSION['uid'];

            $arr = D('gift_log')->where($condition)->findAll();

            foreach ($arr as $key => $val) {

                $gift = $this->Api->get_gift($val[gift_id]);
                $gift_name = $gift['gift_name'];
                $arr[$key]['gift_name'] = $gift_name;

                $sum += $val[worth];
            }

            $this->assign('ret', $arr);
            $this->assign('sum', $sum);
            $this->assign('start_time', $time_start);
            $this->assign('end_time', $time_end);
            // return print_r($ret);
        }

        $this->display();
    }

    public function gains_charge_act() {

        if ($_SESSION['uid']) {


            $start_time = ($_REQUEST['start_time']);

            $end_time = ($_REQUEST['end_time']);

            $condition['give_time'] = array(array('egt', $start_time), array('elt', $end_time));

            $condition[to_id] = $_SESSION['uid'];

            $condition[type] = 0;

            $arr = D('gift_log')->where($condition)->findAll();

            foreach ($arr as $key => $val) {

                $gift = $this->Api->get_gift($val[gift_id]);
                $gift_name = $gift['gift_name'];
                $arr[$key]['gift_name'] = $gift_name;

                $sum += $val[worth];
            }

            $start_id = $arr[0]['id'];

            $len = count($arr);

            $end_id = $arr[$len - 1]['id'];


            $data = array(
                'type' => 1,
            );
            $res = M('gift_log')->where($condition)->save($data);

            $data = array(
                'uid' => $_SESSION['uid'],
                'real_amount' => $sum,
                'type' => 1,
                'start_time' => $start_time,
                'end_time' => $end_time,
                'act_time' => time(),
                'start_id' => $start_id,
                'end_id' => $end_id,
            );
            if ($sum > 0) {
                $info = M('charge_log')->add($data);
            }


            ($info && $res) ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
        }
    }
 public function charge() {
       $this->navigation('在线充值');

        $members[username] = $_SESSION['username'];
        $members[uid] = $_SESSION['uid'];

        // print_r($members);exit();
        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            
            
            
            $this->display();
            
            
        }
     
     
     
 }
 public function charge_act() {

        $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];

        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            $balance=$_REQUEST['balance'];
            $map[uid]= $_SESSION['uid'];
            $data=array(
                 'balance' => $balance,
                
            );
        $res = D("Members")->setInc( 'balance','uid='.$uid,$balance);
        }
         
        
      $res ? $this->Api->successres("充值成功") : $this->Api->faileres("充值失败!");
     
 }
 
 public function message_json() {
     
    $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];

        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            
            $tempArr = $this->Api->get_members($_SESSION['uid']);
            $message_ids = $tempArr[message_ids];
            
            $message_ids = substr($message_ids,0,strlen($message_ids)-1); 
        
            $map['id'] = array('in', $message_ids); 
             $list = D('message')->where($map)->order('id desc')->findAll();
             
            $list  ? $this->Api->successres("充值成功",$list) : $this->Api->faileres("充值失败!");
             
             
            
            
            
        }
     
     
     
     
 }
 
 
 public function message() {
      $this->navigation('短消息');
        $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];

        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
            
            $tempArr = $this->Api->get_members($_SESSION['uid']);
            $message_ids = $tempArr[message_ids];
            
            $message_ids = substr($message_ids,0,strlen($message_ids)-1); 
        
            $map['id'] = array('in', $message_ids); 
             $list = D('message')->where($map)->order('id desc')->findAll();
             
             $this->assign('list', $list);
             
             
             $this->display();
            
            
            
        }
     
     
     
     
 }
 
 
 
 public function review() {
      $this->navigation('我要当主播');
        $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];

        if (!empty($members[username])) {
            $this->assign('Example_username', $members[username]);
            $this->assign('Example_uid', $members[uid]);
           
             
             $this->display();
            
            
            
        }
 }
 
 public function review_act() {
        $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];

        if (!empty($members[username])) {
                             $data = array(
                                'review' => 1,
                            );
            $condition[uid]=$_REQUEST[uid];
            $res = M('members')->where($condition)->save($data);            
            
        }
        
       $res ? $this->Api->successres("申请成功") : $this->Api->faileres("申请失败!");
 }
 
  public function ajax_upload_img(){
        //上传图片
        if (!empty($_FILES['img']['name'])) {            
      $res = $this->_upload($_FILES['img']);
     $res ? $this->Api->successres("上传成功",$res) : $this->Api->faileres("上传失败!");
        } 
         $this->Api->faileres("上传失败!");
    }
    
    
      public function identity_authentication(){
          
             $full_name= $_POST['full_name'];

            $id_card = $_POST['id_card'];
            
            $img = $_POST['img'];
            
              $data = array(
                'uid' => $_SESSION['uid'],
                'full_name' => $full_name,
                  'id_card' => $id_card,
                'img' => $img,
            );
                $res = M('identity')->add($data);
          
           ($res) ? $this->Api->successres("提交成功,请等待审核!") : $this->Api->faileres("操作失败!");
  
    }
 
 
}

?>
